package org.jeecg.modules.demo.controller;


import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.RandomUtil;
import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.exceptions.ClientException;
import com.jeecg.dingtalk.api.base.JdtBaseAPI;
import com.jeecg.dingtalk.api.core.vo.AccessToken;
import com.jeecg.dingtalk.api.message.JdtMessageAPI;
import com.jeecg.dingtalk.api.message.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.htmlcleaner.CleanerProperties;
import org.htmlcleaner.HtmlCleaner;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.util.DySmsEnum;
import org.jeecg.common.util.DySmsHelper;
import org.jeecg.config.thirdapp.ThirdAppConfig;
import org.jeecg.modules.demo.entity.TestEmailEntity;
import org.jeecg.modules.demo.entity.TestSmsEntity;
import org.jeecg.modules.message.handle.impl.EmailSendMsgHandle;
import org.jeecg.modules.system.entity.SysThirdAccount;
import org.jeecg.modules.system.service.ISysThirdAccountService;
import org.jeecg.modules.system.service.impl.ThirdAppDingtalkServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("/sys/demo")
@Api(tags="接口测试demo")
public class TestController {
    @Autowired
    ThirdAppConfig thirdAppConfig;
    
    @Autowired
    ThirdAppDingtalkServiceImpl dingtalkService;
    
    @Autowired
    private ISysThirdAccountService sysThirdAccountService;
    
    public final String THIRD_TYPE = ThirdAppConfig.DINGTALK.toLowerCase();
    
    //测试发送验证码
@PostMapping("/sendSms")
@ApiOperation(value="测试短信发送", notes="测试短信发送")
    public Result sendSms(@RequestBody TestSmsEntity testSmsEntity) throws ClientException {
    String code = RandomUtil.randomNumbers(6);
    JSONObject js = new JSONObject();
    js.put("smsCode", code);
    if(ObjectUtil.isEmpty(testSmsEntity.getTemplateParamJson())){
        testSmsEntity.setTemplateParamJson(js);
    }
    if(ObjectUtil.isEmpty(testSmsEntity.getDySmsEnum())){
        testSmsEntity.setDySmsEnum(DySmsEnum.REGISTER_TEMPLATE_DHHS_CODE);
    }
    return Result.ok(DySmsHelper.sendSms(testSmsEntity.getPhone(), testSmsEntity.getTemplateParamJson(), testSmsEntity.getDySmsEnum()));
}

    //测试发送邮箱
    @PostMapping("/sendEmail")
    @ApiOperation(value="测试邮箱发送", notes="测试邮箱发送")
    public Result sendSms(@RequestBody TestEmailEntity testEmailEntity){
        EmailSendMsgHandle emailSendMsgHandle = new EmailSendMsgHandle();
        emailSendMsgHandle.sendMsg(testEmailEntity.getEs_receiver(), testEmailEntity.getEs_title(), testEmailEntity.getEs_content());
        return Result.ok();
    }
    
    
    //测试发送钉钉图片
    @PostMapping("/sendImg")
    @ApiOperation(value="测试钉钉发送图片消息", notes="测试钉钉发送图片消息")
    public Result sendImg(@RequestBody JSONObject params, HttpServletRequest request) {
        String appKey = thirdAppConfig.getDingtalk().getClientId();
        String appSecret = thirdAppConfig.getDingtalk().getClientSecret();
        AccessToken accessToken = JdtBaseAPI.getAccessToken(appKey, appSecret);
        String access_token = accessToken.getAccessToken();
        String receiver = params.getString("receiver");
        String[] toUsers = receiver.split(",");
        String content = params.getString("content").toString();
            int agentId = thirdAppConfig.getDingtalk().getAgentIdInt();
            Message<ImageMessage> imageMessage = new Message<>(agentId, new ImageMessage(content));
            List<SysThirdAccount> thirdAccountList = sysThirdAccountService.listThirdUserIdByUsername(toUsers, THIRD_TYPE);
            List<String> dtUserIds = thirdAccountList.stream().map(SysThirdAccount::getThirdUserId).collect(Collectors.toList());
            imageMessage.setUserid_list(dtUserIds);
        return Result.ok(JdtMessageAPI.sendImageMessage(imageMessage, access_token));
    }
    
    
    //测试发送钉钉文件
    @PostMapping("/sendFile")
    @ApiOperation(value="测试钉钉发送文件消息", notes="测试钉钉发送文件消息")
    public Result sendFile(@RequestBody JSONObject params, HttpServletRequest request) {
        String appKey = thirdAppConfig.getDingtalk().getClientId();
        String appSecret = thirdAppConfig.getDingtalk().getClientSecret();
        AccessToken accessToken = JdtBaseAPI.getAccessToken(appKey, appSecret);
        String access_token = accessToken.getAccessToken();
        String receiver = params.getString("receiver");
        String[] toUsers = receiver.split(",");
        // 消息内容
        String content = params.getString("content");
        int agentId = thirdAppConfig.getDingtalk().getAgentIdInt();
        Message<FileMessage> fileMessage = new Message<>(agentId, new FileMessage(content));
        List<SysThirdAccount> thirdAccountList = sysThirdAccountService.listThirdUserIdByUsername(toUsers, THIRD_TYPE);
        List<String> dtUserIds = thirdAccountList.stream().map(SysThirdAccount::getThirdUserId).collect(Collectors.toList());
        fileMessage.setUserid_list(dtUserIds);
        return Result.ok(JdtMessageAPI.sendFileMessage(fileMessage, access_token));
    }
    
    //测试发送钉钉文件
    @PostMapping("/sendlink")
    @ApiOperation(value="测试钉钉发送链接消息", notes="测试钉钉发送链接消息")
    public Result sendlink(@RequestBody JSONObject params, HttpServletRequest request) {
        String appKey = thirdAppConfig.getDingtalk().getClientId();
        String appSecret = thirdAppConfig.getDingtalk().getClientSecret();
        AccessToken accessToken = JdtBaseAPI.getAccessToken(appKey, appSecret);
        String access_token = accessToken.getAccessToken();
        String receiver = params.getString("receiver");
        String[] toUsers = receiver.split(",");
        // 消息内容
        String messageUrl = params.getString("messageUrl");
        String picUrl = params.getString("picUrl");
        String title = params.getString("title");
        String text = params.getString("text");
        int agentId = thirdAppConfig.getDingtalk().getAgentIdInt();
        Message<LinkMessage> linkMessage = new Message<>(agentId, new LinkMessage(messageUrl, picUrl, title, text));
        List<SysThirdAccount> thirdAccountList = sysThirdAccountService.listThirdUserIdByUsername(toUsers, THIRD_TYPE);
        List<String> dtUserIds = thirdAccountList.stream().map(SysThirdAccount::getThirdUserId).collect(Collectors.toList());
        linkMessage.setUserid_list(dtUserIds);
        return Result.ok(JdtMessageAPI.sendLinkMessage(linkMessage, access_token));
    }
    
    
    @PostMapping("/sendcard")
    @ApiOperation(value="测试钉钉发送链接消息", notes="测试钉钉发送链接消息")
    public Result sendcard(@RequestBody JSONObject params, HttpServletRequest request) {
        String appKey = thirdAppConfig.getDingtalk().getClientId();
        String appSecret = thirdAppConfig.getDingtalk().getClientSecret();
        AccessToken accessToken = JdtBaseAPI.getAccessToken(appKey, appSecret);
        String access_token = accessToken.getAccessToken();
        String receiver = params.getString("receiver");
        String[] toUsers = receiver.split(",");
        // 消息内容
        String markdown = params.getString("markdown");
        CleanerProperties props = new CleanerProperties();
        String result = new HtmlCleaner(props).clean(markdown).getText().toString();
        String single_title = params.getString("single_title");
        String title = params.getString("title");
        String single_url = params.getString("single_url");
        int agentId = thirdAppConfig.getDingtalk().getAgentIdInt();
        ActionCardMessage message = new ActionCardMessage(result);
        message.setTitle(title);
        message.setSingle_title(single_title);
        message.setSingle_url(single_url);
        Message<ActionCardMessage> actionCardMessage = new Message<>(agentId, message);
        List<SysThirdAccount> thirdAccountList = sysThirdAccountService.listThirdUserIdByUsername(toUsers, THIRD_TYPE);
        List<String> dtUserIds = thirdAccountList.stream().map(SysThirdAccount::getThirdUserId).collect(Collectors.toList());
        actionCardMessage.setUserid_list(dtUserIds);
        return Result.ok(JdtMessageAPI.sendActionCardMessage(actionCardMessage, access_token));
    }
    
}
